var express = require('express');
var router = express.Router();
const UniSMS = require('unisms').default
let {UsersModel,videoMOdel}=require('../../model/model.js')
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/addlogin',async(req,res)=>{
     await UsersModel.create(req.query)
    res.send({
      code:200,
      msg:'注册成功'
    
    })
  })

  router.get('/addvideo',async(req,res)=>{
    await videoMOdel.create(req.query)
    res.send({
      code:200,
      msg:'添加成功'
    })
  })

  router.get('/getvideo',async(req,res)=>{
    let data=await videoMOdel.find()
    // console.log('====================================');
    // console.log(data,new Date());
    // console.log('====================================');
    res.send({
      code:200,
      data:data
    })
  })

   //登录
router.post('/getyzm', async (req, res) => {
  // 初始化
  const client = new UniSMS({
    accessKeyId: 'jtSDCWuXQMDMruxAEH2HKjU2Y9DfYSpDQroYF6J3entHbDXtj', 
    accessKeySecret: 'P9Yn7Mda4rqYM5CYs2u3RnmucgPsBz', //当你实名认证之后就会自动生成ID，直接复制就行
  })
  //接收前端发送过来的手机号
  const  phone  = req.body.tell
  // 生成随机的四位数验证码，也可以修改成其他几位数验证码
  const len = Math.floor(1000 + Math.random() * 9000)
 
  // 发送短信
  client.send({
    to: phone,
    signature: '王梦菡',
    templateId: 'pub_verif_short2', //短信报备中模板管理，在里面随便复制一个通道就可以
    templateData: {
      code: len,
    },
  })
    .then(ret => {
      res.send({ data:len}); //向前端发送生成的验证码
    })
    .catch(e => {
      console.error(e)
    })
})

router.post('/changepassword',async(req,res)=>{
  console.log('====================================');
  console.log(req.body);
  console.log('====================================');
  await UsersModel.find({_id:req.body._id}).updateOne({password:req.body.password})
  let data=await UsersModel.find({_id:req.body._id})
  console.log('====================================');
  console.log(data[0]);
  console.log('====================================');
  res.send({
    data:data[0]
  })
  
})



  router.post('/getlogin',async(req,res)=>{
    let data=await UsersModel.find({_id:req.body._id})
    res.send({
      code:200,
      data:data[0]
    })
  })


module.exports = router;
